import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ToastController } from 'ionic-angular';


import { Https } from '../../app/http.service'

/**
 * Generated class for the SetpaypwdPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-setpaypwd',
  templateUrl: 'setpaypwd.html',
})
export class SetpaypwdPage {
	sendCaptUrl = new Https().Url + "act=connect&op=get_sms_appcaptchas"
	getUserPhoneUrl = new Https().Url + "act=goods&op=app_get_user_phone"
	checkCaptUrl = new Https().Url + "act=goods&op=check_auto_appcode"
	changePayPwdtUrl = new Https().Url + "act=goods&op=app_change_paypwd"
	userName:string = "156****4567";
	capt = 123456;
	captTime = 60
	text:string;
	pwd = [];
	twoPwd = []; 
	active = 0; 
	captIndex = 0; 
	againPwdStute : number = 0;
	pwdtext = '请输入支付密码';
	cap = []
	tufa = 0
	msgText="获取验证码"
	constructor(public navCtrl: NavController, public navParams: NavParams, public toastCtrl: ToastController) {
		let thin = this
		fetch(this.getUserPhoneUrl, {
			method: 'post',
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			body: "mid=" + localStorage.getItem("mid") + "&token=" + localStorage.getItem("token"),
		}).then(function (response) {
			response.text().then(function (res) {
				if (res != '') {
					let data = JSON.parse(res).datas;
					if (data == '3') {
						thin.alertPop('网络错误')
					}else{
						thin.userName = data.member_mobile;
						// thin.sendcapt(thin.userName)
						console.log(data)
					}

				} else {
					thin.alertPop('网络错误')
				}

			})

		}).catch(function (err) {
			// 出错了;等价于 then 的第二个参数,但这样更好用更直观 :(
			thin.alertPop(err)
		});
	} 

	ionViewDidLoad() {
		// this.timeOut()
	}
	timeOut(){
		let that = this;
		let setTime = setInterval(() => {
			that.captTime = that.captTime - 1
			if (that.captTime == 0) {
				clearInterval(setTime)
			}
			// console.log(that.captTime);

		}, 1000)
	}
	//再次发送
	trySend(){
		this.captTime = 60;
		this.timeOut()
		this.sendcapt(this.userName)
	}
	firstSend(){
		this.tufa = 1
		this.timeOut()
		this.sendcapt(this.userName)
	}
	//验证验证码
	inputCapt(text,first,two){
			console.log(text)
			let thin = this
			fetch(this.checkCaptUrl, {
				method: 'post',
				headers: {
					'Content-Type': 'application/x-www-form-urlencoded'
				},
				body: "mid=" + localStorage.getItem("mid") + "&token=" + localStorage.getItem("token") + "&auth_code=" + text,
			}).then(function (response) {
				response.text().then(function (res) {
					if (res != '') {
						let data = JSON.parse(res).code;
						if (data == '0') {
							first.style = "display:none"
							two.style = "display:flex";
						} else {
							thin.alertPop('输入错误，请再次输入')
						}
					} else {
						thin.alertPop('网络错误')
					}

				})

			}).catch(function (err) {
				// 出错了;等价于 then 的第二个参数,但这样更好用更直观 :(
				thin.alertPop(err)
			});
	}
	//验证码
	captInfo(int, event, first, two) { 
		console.dir(int)
		setTimeout(()=>{
			let inval = int.value
			if (inval.length == 6) {
				this.inputCapt(inval, first, two)
			}
		},100)
		
	}
	fucus(inps){
		inps.focus()
		console.log(inps)
	}
	varifyCa(int, event, first, two) {	
		if (event.keyCode == 8) {
			this.cap.pop()
			this.captIndex = this.cap.length;
			if (this.captIndex >= 0) {
				if (this.cap.length == 1) {
					this.cap = [];
				}
			}
			console.log(this.captIndex)
			if (this.captIndex != 0) {
				int.children[this.captIndex - 1].selectionEnd = 1
				// int.children[this.captIndex].focus()

			} else {
				// int.children[0].focus()
			}

		}

	}
	onclic(intT,val){
		if (this.cap.length >= 6){
			intT.children[5].focus()		
			intT.children[5].selectionEnd = 1		
		}else{
			intT.children[this.cap.length].focus()			
		}
	}
	//587628Z	

	intkey(int,inp,event){
		event.preventDefault();
		
		if (this.againPwdStute == 0){
			// this.pwd[this.active] = String.fromCharCode(event.charCode)
			this.pwd[this.active] = event.data
			
			if (this.pwd.length == 6) {
				// console.log(this.pwd);
				int.children[this.pwd.length - 1].className = "circle"
				this.twoPwdPage(int,inp);
			} else if (this.pwd.length < 6) {
				int.children[this.pwd.length - 1].className = "circle"
				this.active++
				// int.children[this.pwd.length].focus()
			}
		} else if (this.againPwdStute == 1){
			this.twoPwd[this.active] = event.data

			if (this.twoPwd.length == 6) {
				// console.log(this.twoPwd);
				let state = 0;
				let thin = this
				let pwd = '';
				int.children[this.twoPwd.length - 1].className = "circle"
				for (let i = 0; i < this.twoPwd.length;i++){
					if (this.twoPwd[i] != this.pwd[i]){
						state = 1
					}
					pwd += this.twoPwd[i];
				}
				if (state == 0){
					fetch(this.changePayPwdtUrl, {
						method: 'post',
						headers: {
							'Content-Type': 'application/x-www-form-urlencoded'
						},
						body: "mid=" + localStorage.getItem("mid") + "&token=" + localStorage.getItem("token") + "&paypwd="+pwd,
					}).then(function (response) {
						response.text().then(function (res) {
							if (res != '') {
								let data = JSON.parse(res).code;
								if (data == '0') {
									thin.alertPop('密码设置成功！')
									localStorage.setItem("payPwd","1");
									thin.navCtrl.pop();
								} else {
									thin.alertPop('出错')
								}
							} else {
								thin.alertPop('网络错误')
							}

						})

					}).catch(function (err) {
						// 出错了;等价于 then 的第二个参数,但这样更好用更直观 :(
						thin.alertPop(err)
					});
					
				}else{
					this.alertPop('密码输入错误！')
					console.log('密码输入错误！')
				}
				
			} else if (this.twoPwd.length < 6) {
				int.children[this.twoPwd.length - 1].className = "circle"
				this.active++
				// int.children[this.twoPwd.length].focus()
			}
			// console.log(this.twoPwd)                                                                                                                                                                                  
		}
		console.log(this.pwd)
		console.log(this.twoPwd)
	}
	backkey(int,event){
		if (event.keyCode == 8 && this.active != 0){
			if (this.againPwdStute == 0){
				this.active = this.pwd.length - 1 <= 0 ? 0 : this.pwd.length - 1;
				if (this.active >= 0){
					int.children[this.active].className = undefined
					this.pwd.pop()
					if (this.pwd.length == 1){
						this.pwd = [];
					}
				}
			} else if (this.againPwdStute == 1){
				this.active = this.twoPwd.length - 1 <= 0 ? 0 : this.twoPwd.length - 1;
				if (this.active >= 0) {
					int.children[this.active].className = undefined
					this.twoPwd.pop()
					if (this.twoPwd.length == 1) {
						this.twoPwd = [];
					}
				}
			}
			
		}
		// console.log(event.keyCode)
		//删除键
		// 
	}

	twoPwdPage(int,inp){
		this.pwdtext ="请再次输入支付密码"
		for(let i = 0; i < 6; i++){
			int.children[i].className = undefined
		}
		inp.focus()
		this.againPwdStute = 1
		this.active = 0
	}

	sendcapt(phone){
		console.log(phone);
		let thin = this
		fetch(this.sendCaptUrl, {
			method: 'post',
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			body: "phone=" + phone,
		}).then(function (response) {
			response.text().then(function (res) {
				if (res != '') {
					let data = JSON.parse(res).Code;
					if (data == '0') {
						thin.alertPop('验证码发送成功')
					} else {
						thin.alertPop('验证码发送失败')
					}
				} else {
					thin.alertPop('网络错误')
				}

			})

		}).catch(function (err) {
			// 出错了;等价于 then 的第二个参数,但这样更好用更直观 :(
			thin.alertPop('验证码发送失败')
		});
	}

	alertPop(err) {
		let toast = this.toastCtrl.create({
			message: err,
			duration: 1000
		}); toast.present();
	}


	isInteger(number) {
		return number >= 0 && String(number).split('.')[1] == undefined
	}
}
